package com.code.controller;

import com.code.service.EmailServiceFeigClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/code")
public class CodeController {
   private Map<String,Object> codeMap = new HashMap<>();
   @Autowired
   private EmailServiceFeigClient emailServiceFeigClient;
    /**
     * 第一步 生成验证码并发送email
     * @param email
     * @return
     */
    @GetMapping("/crate/{email}")
    public Map createCode(@PathVariable String email){
     Map<String,Object> result = new HashMap<>();
//     生成随机数
     String code = "";
        Random random = new Random();
        for (int i = 0; i <8; i++) {
            code += random.nextInt(100);
        }
        codeMap.put(email,code);
//        发送邮件
        Map ret = emailServiceFeigClient.sendEmail(email, code);
        if(ret.get("success").equals("0")){
            //生成token
        }

        return result;
    }

    /**
     * 校验验证码是否正确
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/valdate/{email}/{code}")
    public Map valdate(@PathVariable String email,@PathVariable String code){
        Map<String,Object> result = new HashMap<>();
//        获取验证码
        String getCode = codeMap.get(email).toString();
        if(getCode.equals(code)){
            result.put("success","0");
        }else {
            result.put("error","1");
        }

        return result;
    }


}
